﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using BLL;
public partial class QuarterPlan_QuarterPlanMeetAdd : System.Web.UI.Page
{
    DataStructure dalDataStructure = new DataStructure();
    WeekDateManager dllWeekDateManager = new WeekDateManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    Authentication dllAuthentication = new Authentication();
    WeekMeetRecord dllWeekMeetRecord = new WeekMeetRecord();

    string path = HttpContext.Current.Server.MapPath("~/") + @"Upload\";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate);
            binddata();
        }
    }

    void binddata()
    {
        string sid = GetID();
        if (sid != "-1")
        {
            object[] _value = new object[2];
            _value[0] = Convert.ToInt32(sid);
            _value[1] = Utility.HttpHelp.getParamsString("department");
            DataTable table = dllWeekMeetRecord.getbyid(_value);

            if (table.Rows.Count != 0)
            {

                string dpid = table.Rows[0]["weekdate"].ToString();
                //  drpQuarterDate.SelectedValue = dpid;
                txtMeetTime.Text = Convert.ToDateTime(table.Rows[0]["meettime"].ToString()).ToShortDateString();
                txtMeetRecord.Text = table.Rows[0]["Meetcontent"].ToString();

                _value = new object[1];
                _value[0] = Convert.ToInt32(dpid);
                DataTable weektable = dllWeekDateManager.getbyid(_value);
                if (weektable.Rows.Count != 0)
                {
                    drpYear.SelectedItem.Text = weektable.Rows[0]["weekyear"].ToString();
                    drpQuarterDate.SelectedItem.Text = weektable.Rows[0]["Quartername"].ToString();
                    drpMonthDate.SelectedItem.Text = weektable.Rows[0]["monthname"].ToString();
                    drpWeekDate.SelectedItem.Text = weektable.Rows[0]["weekname"].ToString();
                    if (table.Rows[0]["MeetPath"].ToString().Length > 0)
                    {
                        string _fileName = table.Rows[0]["MeetPath"].ToString();
                        this.lblAttachmentPath.Visible = true;
                        this.lblAttachmentPath.Text = "附件名称为:" + _fileName;
                        this.fupFujian.Visible = false;
                        this.btnDisplay.Visible = true;
                    }
                }



            }
        }
    }

    string GetID()
    {
        string sid = "-1";
        if (Request.Params["id"] != null && Request.Params["id"].ToString().Length != 0)
        {
            sid = Request.Params["id"].ToString();
        }
        return sid;
    }
    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            if (Request.QueryString["Year"] == null)
            {
                DataTable _year = dllQuarterDateManager.getQuarterDate();
                this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));
            }
            else
            {
                this.drpYear.SelectedValue = Utility.HttpHelp.getParamsString("Year");
            }
        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                if (Request.QueryString["QuarterDate"] == null)
                {
                    #region dropdownlist显示系统所对应的当前年周期
                    //根据系统当前日期查找到对应的年周期
                    DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                    //如果查到结果（结果只有一条）

                    if (_quartertime.Rows.Count == 1)
                    {
                        this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                        this.drpQuarterDate.DataSource = _quartertime;
                        this.drpQuarterDate.DataTextField = "QuarterName";
                        this.drpQuarterDate.DataValueField = "Quarter_Id";
                    }
                    #endregion
                }
                else
                {
                    this.drpQuarterDate.SelectedValue = Utility.HttpHelp.getParamsString("QuarterDate");
                }
                bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem li = new ListItem("无年周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);
                bindDropDownList(this.drpMonthDate);

            }
        }


        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                if (Request.QueryString["MonthDate"] == null)
                {
                    #region 根据系统当前日期绑定到当前月周期
                    DataTable _monthtime = dllMonthDateManager.getMonthDate();
                    if (_monthtime.Rows.Count > 0)
                    {
                        this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));

                    }
                    #endregion
                }
                else
                {
                    this.drpMonthDate.SelectedValue = Utility.HttpHelp.getParamsString("MonthDate");
                }
                bindDropDownList(this.drpWeekDate);
            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
                bindDropDownList(this.drpWeekDate);
            }

        }
        //绑定周计划周期
        if (_drp.Equals(this.drpWeekDate))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            _value[2] = this.drpMonthDate.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                this.drpWeekDate.DataSource = _week;
                this.drpWeekDate.DataTextField = "WeekName";
                this.drpWeekDate.DataValueField = "Week_Id";
                this.drpWeekDate.DataBind();
                if (Request.QueryString["WeekDate"] == null)
                {
                    DataTable _weektime = dllWeekDateManager.getWeekDate();
                    if (_weektime.Rows.Count > 0)
                    {
                        this.drpWeekDate.SelectedIndex = this.drpWeekDate.Items.IndexOf(this.drpWeekDate.Items.FindByText(_weektime.Rows[0][4].ToString()));


                    }
                }
                else
                {
                    this.drpWeekDate.SelectedValue = Utility.HttpHelp.getParamsString("WeekDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无周周期");
                this.drpWeekDate.Items.Clear();
                this.drpWeekDate.Items.Add(_li);

            }
        }
    }



    /// <summary>
    /// 添加月会议记录
    /// </summary>
    void addWeekMeetRecord()
    {
        if (this.fupFujian.FileName.Length > 0)
        {
            //获得附件文件名
            string _filename = this.fupFujian.FileName;
            //判断是否存在
            if (System.IO.File.Exists(path + _filename))
            {
                System.IO.File.Delete(path + _filename);
            }
            else
            {
                //弹出对话框报已存在！
            }
            this.fupFujian.SaveAs(path + _filename);
            //构造数组
            object[] _value = new object[5];
            _value[0] = Convert.ToInt32(this.drpWeekDate.SelectedItem.Value);
            _value[1] = Convert.ToDateTime(txtMeetTime.Text);
            _value[2] = txtMeetRecord.Text;
            _value[3] = _filename;
            _value[4] = Utility.HttpHelp.getParamsString("department");
            int result = dllWeekMeetRecord.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加会议记录未成功，请重试！");
            }
        }
        else
        {
            //构造数组
            object[] _value = new object[5];
            _value[0] = Convert.ToInt32(this.drpWeekDate.SelectedItem.Value);
            _value[1] = Convert.ToDateTime(txtMeetTime.Text);
            _value[2] = txtMeetRecord.Text;
            _value[3] = "";
            _value[4] = Utility.HttpHelp.getParamsString("department");
            int result = dllWeekMeetRecord.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加会议记录未成功，请重试！");
            }
        }
    }


    /// <summary>
    /// 修改周会议记录
    /// </summary>
    void updateWeekMeetRecord()
    {
        if (this.fupFujian.FileName.Length > 0 )
        {
            //获得附件文件名
            string _filename = this.fupFujian.FileName;
            //判断是否存在
            if (System.IO.File.Exists(path + _filename))
            {
                System.IO.File.Delete(path + _filename);
            }
            else
            {
                //弹出对话框报已存在！
            }
            this.fupFujian.SaveAs(path + _filename);
            //构造数组
            object[] _value = new object[5];
            _value[0] = this.txtMeetRecord.Text;
            _value[1] = _filename;
            _value[2] = Convert.ToDateTime(this.txtMeetTime.Text);
            _value[3] = Utility.HttpHelp.getParamsString("department");
            _value[4] = Utility.HttpHelp.getParamsInt("Id");
            int result = dllWeekMeetRecord.edit(MonthMeetRecord.field.MeetTime.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("自动保存会议记录未成功，请重试！");
            }
        }
        else
        {
            //构造数组
            object[] _value = new object[5];
            _value[0] = this.txtMeetRecord.Text;
            _value[1] = "";
            _value[2] = Convert.ToDateTime(this.txtMeetTime.Text);
            _value[3] = Utility.HttpHelp.getParamsString("department");
            _value[4] = Utility.HttpHelp.getParamsInt("Id");
            int result = dllWeekMeetRecord.edit(MonthMeetRecord.field.MeetTime.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("自动保存会议记录未成功，请重试！");
            }
        }

    }



    /// <summary>
    /// 添加月会议记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btntijiao_Click(object sender, EventArgs e)
    {
        try
        {
            if(Utility.HttpHelp.getParamsString("Id").Length>0)
            {

                updateWeekMeetRecord();
            }
            else
            {
                addWeekMeetRecord();
            }
        }
        catch
        { }

        Utility.HttpHelp.redirectPage("WeekPlanMeet.aspx");
    }


    /// <summary>
    /// Timer自动保存事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (this.txtMeetTime.Text.Length > 0)
        {
            ////判断是否存在
            //object[] _value = new object[1];
            //_value[0] = Convert.ToDateTime(this.txtMeetTime.Text);
            //DataTable _meetRecord = dllWeekMeetRecord.get(WeekMeetRecord.field.MeetTime.ToString(), _value);
            //if (_meetRecord.Rows.Count > 0)
            //{
            //    updateWeekMeetRecord();
            //}
            //else
            //{
            //    addWeekMeetRecord();
            //}
        }
    }

    //返回方法
    protected void btnchongzhi_Click(object sender, EventArgs e)
    {
        Utility.HttpHelp.redirectPage("WeekPlanMeet.aspx");
    }


    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
    }


    /// <summary>
    /// 年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
    }


    /// <summary>
    /// 月周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpWeekDate);
    }

    /// <summary>
    ///  重新上传事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        this.lblAttachmentPath.Visible = false;
        this.lblAttachmentPath.Text = string.Empty;
        this.fupFujian.Visible = true;
        this.btnDisplay.Visible = false;
    }
}
